package com.gxuwz.check.dao.admin.impl;

import com.gxuwz.check.base.BaseDao;
import com.gxuwz.check.dao.admin.IClassDao;
import com.gxuwz.check.model.dto.ClassDto;
import com.gxuwz.check.model.entity.SysClassEntity;
import com.gxuwz.check.model.entity.SysCollegeEntity;
import com.gxuwz.check.util.PageBean;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Repository
public class ClassDaoImpl extends BaseDao implements IClassDao {

    @Override
    //@Transactional
    public List<ClassDto> list(int page) {
        System.out.println("page class "+page);
        Criteria criteria = getSession().createCriteria(SysClassEntity.class);
        criteria.setFirstResult((page - 1)* new PageBean<>().getPageSize());
        criteria.setMaxResults(new PageBean<>().getPageSize());
        List<SysClassEntity> list = criteria.list();
        System.out.println("list   "+list);
        List<ClassDto> listDto = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            try {
                ClassDto classDto = new ClassDto();
                String hql = "select c.collegeName from SysCollegeEntity c where collegeId = ?";
                List list1 = getHibernateTemplate().find(hql, Integer.valueOf(list.get(i).getCollegeId()));
                classDto.setClassId(list.get(i).getClassId());
                classDto.setClassName(list.get(i).getClassName());
                classDto.setCollegeName((String) list1.get(0));
                listDto.add(classDto);
            }catch (RuntimeException e){
                e.printStackTrace();
            }
        }
        SessionClose();
        return listDto;
    }

    @Override
    public void delete(Integer classId) {
        String sql = "delete from sys_class where class_id = '" + classId + "'";
        getSession().createSQLQuery(sql).executeUpdate();
        SessionClose();
    }

    @Override
    public void edit() {

    }

    @Override
    public void save(String className, int collegeId) {
        String sql = "insert into sys_class(class_name,college_id) value('" + className + "','" + collegeId + "' ) ";
        getSession().createSQLQuery(sql).executeUpdate();
        SessionClose();
    }

    @Override
    @Transactional
    public ClassDto findClassById(int classId) {
        Criteria criteria = getSession().createCriteria(SysClassEntity.class);
        criteria.add(Restrictions.eq("classId", classId));
        List<SysClassEntity> list = criteria.list();

        Criteria criteria1 = getSession().createCriteria(SysCollegeEntity.class);
        criteria1.add(Restrictions.eq("collegeId", list.get(0).getCollegeId()));
        List<SysCollegeEntity> list1 = criteria1.list();

        ClassDto classDto = new ClassDto();
        classDto.setClassId(list.get(0).getClassId());
        classDto.setCollegeId(list.get(0).getCollegeId());
        classDto.setClassName(list.get(0).getClassName());
        classDto.setCollegeName(list1.get(0).getCollegeName());
        SessionClose();
        return classDto;
    }
}
